import db from '../db/index.js'
export function getcourselist(req, res) {
    const { pagesize, pagenum } = req.body
    const page = (pagenum - 1) * pagesize
    const sql = `select  courseid,cname,ctime,credit,type,teacher.name as teacher from course inner join teacher on teacher.teacherid = course.teacherid   limit ${pagesize} offset ${page}`
    db.query(sql, (err, data) => {
        if (err) return res.dbno(err)
        const course = data
        const sql2 = 'select count(*) as total from course'
        db.query(sql2, (err, data) => {
            if (err) return res.dbno(err)
            res.send({ status: 200, course, total: data[0].total })

        })
    })
}
export function seachcourse(req, res) {
    const { pagesize, pagenum, condition, val, } = req.body
    const page = (pagenum - 1) * pagesize
    const sql = `select  courseid,cname,ctime,credit,type,teacher.name as teacher from course
     inner join teacher on teacher.teacherid = course.teacherid  where ${condition}= ? limit ${pagesize} offset ${page} `
    db.query(sql, val, (err, data) => {
        if (err) return res.dbno(err)
        const course = data
        const sql2 = `select count(*) as total from course where ${condition}= ?`
        db.query(sql2,val ,(err, data) => {
            if (err) return res.dbno(err)
            res.send({ status: 200, course, total: data[0].total })
        })
    })

}